package com.hangwei.aicabinet;

import android.app.Application;
import android.os.Handler;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.hangwei.aicabinet.manager.NetManager;
import com.hangwei.aicabinet.model.bean.Login;
import com.hangwei.aicabinet.net.ResponseInfo;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.util.Tools;

import rx.Observable;

/**
 * author: 张龙臣
 * date: 2018-08-28.
 */

public class App extends Application {

    private static App instance;
    private static Handler mHandler;

    public static int tempLock = 0;
    public static boolean isAdmin = false;

    public static App getInstance() {
        return instance;
    }

    public static Handler getmHandler() {
        return mHandler;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        mHandler = new Handler();
        Tools.init(this);
        Fresco.initialize(this);
        wakeUpNetWork();
    }

    private void wakeUpNetWork() {
        NetManager.getInstance().doSubscribe(new NetManager.Model<Login>() {
            @Override
            protected Observable<ResponseInfo<Login>> getObservable() {
                return NetManager.getResponseInfoAPI().listenLogin(SPUtil.getString(SPKey.CABINET_ID));
            }

            @Override
            protected void onNext(Login data) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        wakeUpNetWork();
                    }
                }, 10 * 60 * 1000);

            }

            @Override
            protected void onError() {
            }
        });
    }

}
